package edu.berkeley.cs160.GroupC;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;

import de.vdheide.mp3.FrameDamagedException;
import de.vdheide.mp3.ID3v2DecompressionException;
import de.vdheide.mp3.ID3v2IllegalVersionException;
import de.vdheide.mp3.ID3v2WrongCRCException;
import de.vdheide.mp3.MP3File;
import de.vdheide.mp3.NoMP3FrameException;
import de.vdheide.mp3.TagContent;

import edu.berkeley.cs160.GroupC.songs.Song;

import android.app.Application;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

class Mp3Filter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3"));
    }
}

public class WorkoutMusicApplication extends Application {
	public static final int LOW_INTENSITY = 1;
	public static final int MEDIUM_INTENSITY = 2;
	public static final int HIGH_INTENSITY = 3;
	public static final int STEPS_MSG = 1010;
	public static final int PACE_MSG = 2102;
	public static float SENSITIVITY = 10f;//10 works pretty good for archos, an probably needs 5-8
	public static int CHANGE_DELAY = 10; //seconds
	
	public Intent mIntent;
	
	private ArrayList<Song> currentPlaylist;
	private Song newSongAdded;
	
	public int currentSteps = 0;
	public int currentPace = 0;
	public boolean serviceIsRunning =false;
	public boolean serviceIsBound = false;
	public int countCreate = 0;
	public int countResume = 0;
	public int countPause = 0;
	public int countCreate2 = 0;
	public int countResume2 = 0;
	public int countPause2 = 0;
	public int countUNBIND = 0;
	

	
	
	
	@Override
	public void onCreate() {
		
		super.onCreate();
		
		if (currentPlaylist == null) {
			currentPlaylist = new ArrayList<Song>();
		}
		
		Log.v("SUS", "Inside onCreate.");

			setUpSongs();

	}

	private void setUpSongs()  {
		
		Log.v("SUS", "Setting up songs");
		
		File home = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/sdcard/");
		//if (null == home) return;
		Mp3Filter temp = new Mp3Filter();
		
		if(home.listFiles(temp)==null) return;
		
		
		if (home.listFiles(new Mp3Filter()).length >0) {
			String artist;
			String title;
			String album;
			String filepath;
			String bpm;
			
    		for (File file : home.listFiles(new Mp3Filter())) {

    			try {
					MP3File testfile = new MP3File(file.getAbsolutePath());
					
					TagContent artistTag = testfile.getArtist();
					artist = artistTag.getTextContent();
					
					TagContent titleTag = testfile.getTitle();
					title = titleTag.getTextContent();
					
					TagContent albumTag = testfile.getAlbum();
					album = albumTag.getTextContent();
					
					TagContent bpmTag = testfile.getBPM();
					bpm = bpmTag.getTextContent();
					//Log.d("QI", "BPM for "+title+" is "+bpm);
					if (bpm==null){
						bpm=("-1.0");
					}
					if(title==null){
						title=file.getName();
					}
					if(artist==null){
						artist="Unknown Artist";
					}
					if(album==null){
						album="Unknown Album";
					}
					
					Song newSong = new Song(artist, album, title, file.getPath(), (int)Float.parseFloat(bpm)); 
					addSong(newSong);
					int curBpm = newSong.getBpm(); 
					if( curBpm != -1){
						if (curBpm < 112){
							newSong.setRating(LOW_INTENSITY);
						}else if (curBpm < 140){
							newSong.setRating(MEDIUM_INTENSITY);
						}else{
							newSong.setRating(HIGH_INTENSITY);
							
						}
						
					}
					
				} catch (ID3v2WrongCRCException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ID3v2DecompressionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ID3v2IllegalVersionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoMP3FrameException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (FrameDamagedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
				} 
    			
    			
				
    		}
		}else
			//Toast.makeText(WorkoutMusicApplication.this, "Length of the listFiles is" + Environment.getExternalStorageDirectory().listFiles( ).length, Toast.LENGTH_SHORT).show();
		Log.v("SUS", "Done setting up songs");
	}

	public void setCurrentPlayList(ArrayList<Song> playlist) {
		this.currentPlaylist = playlist;
	}

	public ArrayList<Song> getCurrentPlaylist() {
		return currentPlaylist;
	}
	
	public void addSong(Song s) {
		assert(s != null);
		currentPlaylist.add(s);
	}

	public void setNewSongAdded(Song newSongAdded) {
		this.newSongAdded = newSongAdded;
	}

	public Song getNewSongAdded() {
		return newSongAdded;
	}

	public int getCurrentSteps() {
		return currentSteps;
	}

	public int getCurrentPace() {
		return currentPace;
	}
	
	public void setCurrentSteps(int s) {
		currentSteps = s;
	}

	public void setCurrentPace(int p) {
		currentPace = p;
	}
	
}
